#include "HttpServer.hpp"
#include "Deamon.hpp"

using namespace LogModule;

// 登入功能
void Login(HttpRequset &req, HttpResponse &resp)
{
    // 根据 req 动态构建 resp: 
    // Path: /login
    // Args: name=zhangsan&password=123456
    LOG(LogLevel::DEBUG) << "进入登入模块: " << req.Path() << ", " << req.Args();

    // 1. 解析参数格式, 得到想要的参数
    std::string req_args = req.Args();

    // 2. 访问数据库, 验证是否是合法用户

    // 3. 登入成功
    // resp.SetCode(302);
    // resp.SetHeader("Location", "/"); // 登入成功后跳转到首页

    std::string body = req.GetContent("wwwroot/success.html");
    resp.SetCode(200);
    resp.SetHeader("Content-Length", std::to_string(body.size()));
    resp.SetHeader("Content-Type", "text/html");
    resp.SetHeader("Set-Cookie", "username=xzy&password=123456");
    resp.SetBody(body);
}

// 注册功能
void Register(HttpRequset &req, HttpResponse &resp)
{
    LOG(LogLevel::DEBUG) << "进入注册模块: " << req.Path() << ", " << req.Args();
}

// 搜索引擎功能
void Search(HttpRequset &req, HttpResponse &resp)
{
    LOG(LogLevel::DEBUG) << "进入注册模块: " << req.Path() << ", " << req.Args();
}

int main(int argc, char *argv[])
{
    // Deamon(false, false); // 守护进程

    if (argc != 2)
    {
        std::cout << "Usage: " << argv[0] << " port" << std::endl;
        return 1;
    }
    int port = std::stoi(argv[1]);

    std::unique_ptr<HttpServer> httpserver = std::make_unique<HttpServer>(port);

    // 服务器具有登入成功功能
    httpserver->Register("/login", Login);
    httpserver->Register("/register", Register);

    httpserver->Start();

    return 0;
}